package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallLogisticsDtoMapper;
import com.starlink.model.domain.MallLogisticsDO;
import com.starlink.mapper.MallLogisticsMapper;
import com.starlink.model.request.MallLogisticsRequest;
import com.starlink.service.MallLogisticsService;
import com.starlink.model.util.MallLogisticsConvert;
import com.starlink.model.vo.MallLogisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class MallLogisticsServiceImpl extends ServiceImpl<MallLogisticsMapper, MallLogisticsDO> implements MallLogisticsService {
    @Autowired
    private MallLogisticsMapper mallLogisticsMapper;

    @Override
    public MallLogisticsVO getByKey(long id) {
        MallLogisticsVO mallLogisticsVO = MallLogisticsDtoMapper.INSTANCE.do2VO(getById(id));
        return mallLogisticsVO;
    }

    @Override
    public PageInfo<MallLogisticsVO> pageQuery(Pageable pageable,MallLogisticsRequest request) {
        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallLogisticsDO> list = mallLogisticsMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallLogisticsVO> mallLogisticsVOList = MallLogisticsConvert.buildMallLogisticsVoList(list);
        return new PageInfo(mallLogisticsVOList);
    }

    @Override
    public List<MallLogisticsVO> listQuery(MallLogisticsRequest request) {
        List<MallLogisticsDO> list = mallLogisticsMapper.selectDOList(MallLogisticsDtoMapper.INSTANCE.request2DO(request));
        List<MallLogisticsVO> mallLogisticsVOS = MallLogisticsConvert.buildMallLogisticsVoList(list);
        return mallLogisticsVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallLogisticsRequest request) {
        request.setId(getId());
        save(MallLogisticsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallLogisticsRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallLogisticsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallLogisticsMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
